package com.matt.sooncleaner.launcher

import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import com.matt.sooncleaner.MainActivity
import com.matt.sooncleaner.R
import com.matt.sooncleaner.arch.ArchActivity
import com.matt.sooncleaner.databinding.ActivityLauncherBinding

class LauncherActivity : ArchActivity<ActivityLauncherBinding>() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val fragment = if (isFirstUse()) PrivacyPolicyFragment {
            showFragment(SplashFragment(this::startMain))
        } else SplashFragment(this::startMain)
        showFragment(fragment)
    }

    private fun startMain() {
        startActivity(Intent(this, MainActivity::class.java))
        finish()
    }

    private fun showFragment(fragment: Fragment) {
        supportFragmentManager
            .beginTransaction()
            .replace(R.id.fragment_container, fragment)
            .commitNowAllowingStateLoss()
    }
}